package au.com.handsunited.jodawrapper;

import java.io.Serializable;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalTime;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author Arun Chandrasekhar
 */
public class JodaLocalTimeWrapper implements Comparable<JodaLocalTimeWrapper>,
		Serializable {


	/**
	 * 
	 */
	private static final long serialVersionUID = -341964169289868759L;

	private LocalTime time;

	private static Logger logger = LoggerFactory
			.getLogger(JodaLocalTimeWrapper.class);

	public JodaLocalTimeWrapper() {
		super();
		time = new LocalTime();
	}

	/**
	 * 
	 * @param hour
	 *            24hr format
	 * @param minute
	 */
	public JodaLocalTimeWrapper(int hour, int minute) {
		this();
		setHour(hour);
		setMinute(minute);
	}

	/**
	 * 
	 * @param time
	 */
	public JodaLocalTimeWrapper(LocalTime time) {
		this.time = new LocalTime(time);
	}

	/**
	 * 
	 * @param timeInMillis
	 */
	public JodaLocalTimeWrapper(long timeInMillis) {
		this.time = new LocalTime(timeInMillis);
	}
	
	/**
	 * 
	 * @param date
	 */
	public JodaLocalTimeWrapper(Date date) {
		this(date.getTime());
	}
	
	
	/**
	 * @return the time
	 */
	public LocalTime getTime() {
		return time;
	}

	/**
	 * 
	 * @return
	 */
	public int getHour() {
		return time.getHourOfDay();
	}

	/**
	 * 
	 * @param hour
	 */
	public void setHour(int hour) {
		time = time.withHourOfDay(hour);
	}

	/**
	 * 
	 * @return
	 */
	public String getHourStr() {
		return StringUtils.leftPad(getHour() + "", 2, '0');
	}

	/**
	 * 
	 * @param hourStr
	 */
	public void setHourStr(String hourStr) {
		if (StringUtils.isNotBlank(hourStr)) {
			setHour(Integer.parseInt(hourStr));
		}
	}

	/**
	 * 
	 * @return
	 */
	public int getMinute() {
		return time.getMinuteOfHour();
	}

	/**
	 * 
	 * @param minute
	 */
	public void setMinute(int minute) {
		time = time.withMinuteOfHour(minute);
	}

	/**
	 * 
	 * @return
	 */
	public String getMinuteStr() {
		return StringUtils.leftPad(getMinute() + "", 2, '0');
	}

	/**
	 * 
	 * @param minuteStr
	 */
	public void setMinuteStr(String minuteStr) {
		if (StringUtils.isNotBlank(minuteStr)) {
			setMinute(Integer.parseInt(minuteStr));
		}
	}

	/**
	 * 
	 * @return
	 */
	public int getSecond() {
		return time.getSecondOfMinute();
	}

	/**
	 * 
	 * @param second
	 */
	public void setSecond(int second) {
		time = time.withSecondOfMinute(second);
	}

	/**
	 * 
	 * @return
	 */
	public String getSecondStr() {
		return StringUtils.leftPad(getSecond() + "", 2, '0');
	}

	/**
	 * 
	 * @param secondStr
	 */
	public void setSecondStr(String secondStr) {
		if (StringUtils.isNotBlank(secondStr)) {
			setSecond(Integer.parseInt(secondStr));
		}
	}

	/**
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(JodaLocalTimeWrapper jltw) {
		return time.compareTo(jltw.getTime());
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return time.toString();
	}

	/**
	 * 
	 * @param formatter
	 * @return
	 */
	public String toString(DateTimeFormatter formatter) {
		return time.toString(formatter);
	}

	/**
	 * 
	 * @param pattern
	 * @return
	 */
	public String toString(String pattern) {
		return time.toString(pattern);
	}

	/**
	 * 
	 * @param pattern
	 * @param locale
	 * @return
	 */
	public String toString(String pattern, Locale locale) {
		return time.toString(pattern, locale);
	}

}
